{% extends 'layout.html' %}

{% block content %}
    <div>
        <h3>任务管理</h3>
        <h4>案例1</h4>
        <input type="button" class="btn btn-primary" value="点击" onclick="clickal1()">
        <p>--------案例2-------------</p>

        <input type="text" id="txtu" placeholder="姓名">
        <input type="text" id="txta" placeholder="年龄">
        <input type="button" class="btn btn-primary" value="点击" id="btn2">

        <p>--------案例3-------------</p>
        <form action="" id="form3">
            <input type="text" name="user" placeholder="姓名">
            <input type="text" name="age" placeholder="年龄">
            <input type="text" name="email" placeholder="邮箱">
            <input type="text" name="more" placeholder="介绍">
        </form>
        <input type="button" id="btn3" value="点击3">
        <p>----------案例4-----------</p>
        <div class="panel panel-default">
            <div class="panel-heading">表单</div>
            <div class="panel-body">
                <form id="form4">
                    {% for field in form %}
                        <div class="form-group col-xs-6" style="position: relative;margin-bottom: 24px">
                            <label>{{ field.label }}</label>
                            {{ field }}
                            <span class="error_msg" style="color: red;position: absolute">{{ field.errors.0 }}</span>
                        </div>
                    {% endfor %}
                    <div class="col-xs-12">
                        <button type="button" id="btn4" class="btn btn-primary">提 交</button>
                    </div>
                </form>
            </div>
        </div>

    </div>

    <div>
        <div class="panel panel-default" style="margin-top: 10px">
            <div class="panel-heading"><span class="glyphicon glyphicon-list"></span> 部门列表</div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in depart_list %}
                    <tr>
                        <td>{{ obj.id }}</td>
                        <td>{{ obj.title }}</td>
                        <td>
                            <a class="btn btn-primary btn-xs" href="/depart/{{ obj.id }}/edit/"
                               style="margin-right: 5px">编辑</a>
                            <a class="btn btn-danger btn-xs" href="/depart/delete/?nid={{ obj.id }}">删除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script type="text/javascript">
        function clickal1() {
            $.ajax({
                url: '/task/ajax/',
                type: 'get',
                data: {
                    n1: 123,
                    n2: 456
                },
                dataType: "JSON",
                success: function (res) {
                    console.log(res.status)
                    console.log(res.data)
                }
            })
        }

        $(function () {
            binBtn2()
            binBtn3()
            binBtn4()
        })

        function binBtn2() {
            $('#btn2').click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: {
                        name: $('#txtu').val(),
                        age: $('#txta').val()
                    },
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res.status)
                        console.log(res.data)
                    }
                })
            })
        }

        function binBtn3() {
            $('#btn3').click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: $('#form3').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res)
                        console.log(res.status)
                        console.log(res.data)
                    }
                })
            })
        }

        function binBtn4() {
            $('#btn4').click(function () {
                $('.error_msg').text('');
                $.ajax({
                    url: '/task/add/',
                    type: 'post',
                    data: $('#form4').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            alert("添加成功")
                        } else {
                            $.each(res.error, function (n, v) {
                                $('#id_' + n).next().text(v[0])
                            })
                        }
                    }
                })
            })
        }


    </script>

{% endblock %}